#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#include <WiFi.h>
#include <HTTPClient.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);

const char* ssid = "trolley";              // 连接WiFi名（此处使用taichi-maker为示例）
const char* password = "12345678";         // 连接WiFi密码（此处使用12345678为示例）
String serverUrl = "http://192.168.4.1/";  //请求的地址

HTTPClient http;  //实例化HTTP对象

// 定义轻触开关引脚
const int switchPin1 = 13;
const int switchPin2 = 12;
const int switchPin3 = 14;
const int switchPin4 = 27;
const int switchPin5 = 26;
const int switchPin6 = 25;

// 定义摇杆引脚
const int lxPin = 33;  // X轴引脚
const int lyPin = 32;  // Y轴引脚

const int rxPin = 35;  // X轴引脚
const int ryPin = 34;  // Y轴引脚

String message = "你好世界！";

void setup() {
  Serial.begin(115200);
  u8g2.begin();
  u8g2.enableUTF8Print();

  //设置引脚模式
  pinMode(switchPin1, INPUT_PULLUP);  // 将轻触开关引脚设置为上拉输入模式
  pinMode(switchPin2, INPUT_PULLUP);  // 将轻触开关引脚设置为上拉输入模式
  pinMode(switchPin3, INPUT_PULLUP);  // 将轻触开关引脚设置为上拉输入模式
  pinMode(switchPin4, INPUT_PULLUP);  // 将轻触开关引脚设置为上拉输入模式
  pinMode(switchPin5, INPUT_PULLUP);  // 将轻触开关引脚设置为上拉输入模式
  pinMode(switchPin6, INPUT_PULLUP);  // 将轻触开关引脚设置为上拉输入模式

  //连接wifi============================================================
  // 连接指定的 Wi-Fi 网络
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_wqy16_t_gb2312b);
    u8g2.setCursor(22, 14);
    u8g2.print("Connecting to WiFi...");
    u8g2.sendBuffer();
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to Wi-Fi!");
  Serial.println(WiFi.softAPIP());  //IP地址
  //连接wifi============================================================
}

int lxValue = 0;
int lyValue = 0;
int rxValue = 0;
int ryValue = 0;

void loop() {
  // 读取轻触开关的状态
  int switchState1 = digitalRead(switchPin1);
  int switchState2 = digitalRead(switchPin2);
  int switchState3 = digitalRead(switchPin3);
  int switchState4 = digitalRead(switchPin4);
  int switchState5 = digitalRead(switchPin5);
  int switchState6 = digitalRead(switchPin6);

  // 读取摇杆X轴和Y轴的模拟值
  int prevlxValue = analogRead(lxPin);
  int prevlyValue = analogRead(lyPin);
  int prevrxValue = analogRead(rxPin);
  int prevryValue = analogRead(ryPin);

  // 抖动消除，求两值之差的绝对值
  if (abs(lxValue - prevlxValue) > 10 || abs(lyValue - prevlyValue) > 10) {
    lxValue = prevlxValue;
    lyValue = prevlyValue;
    sendGetRequest(serverUrl + "?speed=" + ceil(lxValue / 4));
  }
  if (abs(rxValue - prevrxValue) > 10 || abs(ryValue - prevryValue) > 10) {
    rxValue = prevrxValue;
    ryValue = prevryValue;
    if(rxValue==0){//下

    }

    if(rxValue>4000){//上

    }

    if(ryValue==0){//左

    }

    if(ryValue==0){//右

    }
  }
  
  // 如果开关被按下(switchState为LOW),则打印信息
  if (switchState1 == LOW) {
    message = "按钮1被按下";
  }
  if (switchState2 == LOW) {
    message = "按钮2被按下";
  }
  if (switchState3 == LOW) {
    message = "按钮3被按下";
  }
  if (switchState4 == LOW) {
    message = "按钮4被按下";
  }
  if (switchState5 == LOW) {
    message = "按钮5被按下";
  }
  if (switchState6 == LOW) {
    message = "按钮6被按下";
  }

  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_wqy16_t_gb2312b);

  u8g2.setCursor(22, 14);
  u8g2.print(message);

  u8g2.setCursor(0, 30);
  u8g2.print("左x:");
  u8g2.print(lxValue);
  u8g2.setCursor(0, 46);
  u8g2.print("左y:");
  u8g2.print(lyValue);

  u8g2.setCursor(65, 30);
  u8g2.print("右x:");
  u8g2.print(rxValue);
  u8g2.setCursor(65, 46);
  u8g2.print("右y:");
  u8g2.print(ryValue);
  u8g2.sendBuffer();
  delay(300);
}

String sendGetRequest(String url) {
  http.begin(url);
  int httpResponseCode = http.GET();
  if (httpResponseCode > 0) {
    String response = http.getString();
    Serial.println(httpResponseCode);
    Serial.println(response);
    return response;
  } else {
    Serial.print("Error on HTTP request: ");
    Serial.println(httpResponseCode);
    return "Error";
  }
  http.end();
}